package engine;

import acm.program.ConsoleProgram;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class Map {
    int nPlaces;                  //地点总数量
    ArrayList<Place>places = new ArrayList<>();       // 保存所有的地点
    ArrayList<String> props = new ArrayList<>();      // 保存地图上所有的道具
    int baggageNum = 0;             //行李里的道具数量
    public Place currPlace;        // 当前所处的地点

    //加载路线
    public void loadRoutes(Scanner scanner) {
        int nRoutes = scanner.nextInt();
        scanner.next();
        for (int i = 0; i < nRoutes; i++) {
            int placeName1 = scanner.nextInt();
            String direction = scanner.next();
            int placeName2 = scanner.nextInt();
            for (int j = 0; j < nPlaces; j++) {
                if (placeName1 == (places.get(j).getId())) {
                    for (int k = 0; k < nPlaces; k++) {
                        if (placeName2 == (places.get(k).getId())) {
                            switch (direction) {
                                case ("东"):
                                    places.get(j).setEast(places.get(k));
                                    places.get(k).setWest(places.get(j));
                                    break;
                                case ("西"):
                                    places.get(j).setWest(places.get(k));
                                    places.get(k).setEast(places.get(j));
                                    break;
                                case ("南"):
                                    places.get(j).setSouth(places.get(k));
                                    places.get(k).setNorth(places.get(j));
                                    break;
                                case ("北"):
                                    places.get(j).setNorth(places.get(k));
                                    places.get(k).setSouth(places.get(j));
                                    break;
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 读取地点
     *
     * @param scanner 用来读取输入的scanner对象
     */
    public void loadPlaces(Scanner scanner) {
        // 读取地点
        nPlaces = scanner.nextInt();            // 地点的数量
//        message = new String[nPlaces];
        scanner.next();
        // 读取所有地点
        for (int i = 0; i < nPlaces; i++) {
            int id = scanner.nextInt();       //地点的编号
            String placeName = scanner.next();// 地点的名称
            String messageName = scanner.next();//信息

            Place place = new Place(placeName);     // 从Place类别中创建一个实例，并
            places.add(place);                      // 将这个地点保存进places中
            place.setMessage(messageName);
            place.setId(id);
        }

        // 设置初始地点，也就是读进来的第一个地点
        currPlace = places.get(0);
    }

    //加载地图上的道具
    public void loadProps(Scanner scanner) {
        int nProps = scanner.nextInt();            //道具的数量
        scanner.next();
        for (int i = 0; i < nProps; i++) {
            int id = scanner.nextInt();
            String prop = scanner.next();
            places.get(id).setProp(prop);
            props.add(prop);
        }
    }

    //获取当前位置的道具
    public String  pickProps(Place place) {
        place.setProp(null);
        baggageNum += 1;
        return("DEBUG2 - 你找到了一个" + place.getProp() + "！");
    }

    //查看背包道具数量
    public String checkBaggageNum() {
        if (baggageNum != 0) {
            return (String.format("你现在共有%d个道具，依次是：", baggageNum));
        } else {
            return ("你的行李箱内空无一物。");
        }
    }

    // 查看背包
    public String checkBaggage() {
        String j = "";                          //返回的语句
        if (baggageNum != 0) {
            for (int i = 0; i < baggageNum; i++) {
                j = j + String.format("%d.%s", i + 1, props.get(i));
            }
        }
        return j;
    }

    //获取当前位置
    public Place getCurrPlace(){
        return currPlace;
    }

    //设置当前位置
    public void setCurrPlace(Place currPlace){
        this.currPlace = currPlace;
    }
}